<?php

namespace app\common\library;


/**
 * 字符串处理
 * @package app\common\library
 */
class Str
{
    /**
     * 过滤html字符
     * @param $content
     * @return mixed
     */
    public static function filterHtml($content)
    {
        $content = preg_replace("@<script(.*?)</script>@is", "", $content);
        $content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
        $content = preg_replace("@<style(.*?)</style>@is", "", $content);
        $content = preg_replace("@<(.*?)>@is", "", $content);
        $content = str_replace(PHP_EOL, '', $content);
        $space = array(" ", "　", "  ", " ", " ");
        $go_away = array("", "", "", "", "");
        $content = str_replace($space, $go_away, $content);
        return $content;
    }


    public static function getNeedBetween($str,$mark1,$mark2)
    {
        $st = stripos($str, $mark1);
        $ed = stripos($str, $mark2);
        if (($st == false || $ed == false) || $st >= $ed)
            return 0;
        $mk1_len = strlen($mark1);
        $str = substr($str, ($st + $mk1_len), ($ed - $st - $mk1_len));
        return $str;
    }

    public static function getSkuTitle($str)
    {
//        $mark1 = '<span class="spec-value-type-text">';
//        $mark2 = "</span></span>";
//        return self::getNeedBetween($str, $mark1, $mark2);
        $arr = explode('：', $str);
        preg_match('/.*>([^<>]+)</', $arr[1],$match);
        return $match[1];
    }

    public static function getSkuSpec($str)
    {
        $str = preg_replace('/<[^>]+>/', ' ',$str);
        $str = implode(' ', array_filter(explode(' ', $str)));
        return preg_replace('/:\s+/',':',preg_replace('/：/',':',$str));
    }
}